package com.vbobot.demo.spring.nacos.client;

import javax.annotation.Resource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @author Bobo
 * @date 2022/9/19
 */
@RestController
public class ClientTestController {
    @Resource NacosServerFeign nacosServerFeign;

    @Resource RestTemplate httpClientRestTemplate;
    @Resource RestTemplate notSupportGetWithBodyRestTemplate;
    @Resource RestTemplate okHttpRestTemplate;
    @Resource RestTemplate okHttpRestTemplate1;
    @Resource RestTemplate okHttpRestTemplate2;

    @GetMapping("/get/by/feign")
    public String helloWorld() {
        return nacosServerFeign.helloWorld();
    }

    @GetMapping("/get/with/body/by/feign")
    public String getWithBodyByFeign() {
        return nacosServerFeign.getWithBody(new GetWithBodyParam("123"));
    }

    @GetMapping("/get/with/body/by/rest")
    public String getWithBody() {
        final String url = "http://spring-demo-nacos-server/get/with/body";
        final HttpEntity<GetWithBodyParam> entity = new HttpEntity<>(new GetWithBodyParam(String.valueOf(Math.random())));
        return httpClientRestTemplate.exchange(url, HttpMethod.GET, entity, String.class).getBody();
    }

    @GetMapping("/get/with/body/by/ok/1")
    public String getWithBodyByOk1() {
        final String url = "http://spring-demo-nacos-server/get/with/body";
        final HttpEntity<GetWithBodyParam> entity = new HttpEntity<>(new GetWithBodyParam(String.valueOf(Math.random())));
        return okHttpRestTemplate1.exchange(url, HttpMethod.GET, entity, String.class).getBody();
    }

    @GetMapping("/get/with/body/by/ok/2")
    public String getWithBodyByOk2() {
        final String url = "http://spring-demo-nacos-server/get/with/body";
        final HttpEntity<GetWithBodyParam> entity = new HttpEntity<>(new GetWithBodyParam(String.valueOf(Math.random())));
        return okHttpRestTemplate2.exchange(url, HttpMethod.GET, entity, String.class).getBody();
    }

    @GetMapping("/get/with/body/by/ok")
    public String getWithBodyByOk() {
        final String url = "http://spring-demo-nacos-server/get/with/body";
        final HttpEntity<GetWithBodyParam> entity = new HttpEntity<>(new GetWithBodyParam(String.valueOf(Math.random())));
        return okHttpRestTemplate.exchange(url, HttpMethod.GET, entity, String.class).getBody();
    }

    @GetMapping("/get/with/body/by/ok/error")
    public String getWithBodyByOkAndReturnError() {
        final String url = "http://spring-demo-nacos-server/get/with/body";
        final HttpEntity<GetWithBodyParam> entity = new HttpEntity<>(new GetWithBodyParam(String.valueOf(Math.random())));
        return notSupportGetWithBodyRestTemplate.exchange(url, HttpMethod.GET, entity, String.class).getBody();
    }

    @GetMapping("/post/with/body/by/ok")
    public String postWithBodyByOk() {
        final String url = "http://spring-demo-nacos-server/post/with/body";
        final HttpEntity<GetWithBodyParam> entity = new HttpEntity<>(new GetWithBodyParam(String.valueOf(Math.random())));
        return okHttpRestTemplate.exchange(url, HttpMethod.POST, entity, String.class).getBody();
    }

    @GetMapping("/get/by/rest")
    public String helloWorldByRest() {
        final String url = "http://spring-demo-nacos-server/hello/world";
        final ResponseEntity<String> exchange = httpClientRestTemplate.exchange(url, HttpMethod.GET, null, String.class);
        return exchange.getBody();
    }

    @GetMapping("/get/by/ok/rest")
    public String helloWorldByOkRest() {
        final String url = "http://spring-demo-nacos-server/hello/world";
        final ResponseEntity<String> exchange = okHttpRestTemplate.exchange(url, HttpMethod.GET, null, String.class);
        return exchange.getBody();
    }

    public static class GetWithBodyParam {
        private String name;

        public GetWithBodyParam(String name) {
            this.name = name;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }
}
